//
//  RJTipPopView.swift
//  FactoryManager-Swift
//
//  Created by 苏州巨细 on 2021/4/14.
//  Copyright © 2021 苏州巨细. All rights reserved.
//

import UIKit

let CancelButtonColor = UIColor.colorWithHexString("646B81")
let ConfirmButtonColor = UIColor.colorWithHexString("02B192")

protocol RJTipPopViewDataSource: NSObjectProtocol {
    func RJTipPopViewDataSourceWithCompletion() -> Any;
}

protocol RJTipPopViewAnimationDelegate: NSObjectProtocol {
    
    func RJTipPopViewAnimationDelegateWithOpen(view:UIView);
    
    func RJTipPopViewAnimationDelegateWithDismiss(view:UIView);
}

class RJTipPopView: UIView {

    /// 内容视图
    var contentView: UIView = UIView()
    /// 确定按钮
    var confirmButton: UIButton = UIButton()
    /// 实现回调
    var completionBlock: ((_ obj: Any?) -> Void)?
    
    var showButton = false
    /// 定制显示视图
    private var childContentView: RJTipContentView!
    /// 回调数据源代理
    private weak var dataSource: RJTipPopViewDataSource?
    /// 显示动画代理
    private weak var animationDelegate: RJTipPopViewAnimationDelegate?

    class func showWithChildContentView(childView:RJTipContentView, showButton:Bool = false,
                                  constraint:@escaping ((_ contentView:UIView)-> Void), completionBlock:@escaping ((_ obj: Any?) -> Void)) -> Void {
        let tipView = RJTipPopView()
        tipView.showButton = showButton
        tipView.completionBlock = completionBlock
        tipView.setupCustomView()
        tipView.addTipContentView(childContentView: childView)
        constraint(tipView.contentView)
        tipView.showTipView()
    }
    
    @objc func confirmButtonClick() {
        if let completionBlock = completionBlock {
            let result = self.dataSource?.RJTipPopViewDataSourceWithCompletion()
            completionBlock(result)
        }
        dismiss()
    }
    
}

//MARK: 设置界面
extension RJTipPopView {
    
    func setupCustomView() -> () {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        
        let bgBtn = UIButton()
        bgBtn.backgroundColor = UIColor.clear
        bgBtn.addTarget(self, action: #selector(dismiss), for: .touchUpInside)
        self.addSubview(bgBtn)
        
        contentView.backgroundColor = ThemeColor
        self.addSubview(contentView)
        
        bgBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        if showButton {
            setupBottomView()
        }
    }
    
    func setupBottomView() -> () {
        
        let cancelButton = UIButton()
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.setTitleColor(Color_BCBCBC, for: .normal)
        cancelButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        cancelButton.addTarget(self, action: #selector(dismiss), for: .touchUpInside)
        cancelButton.backgroundColor = CancelButtonColor
        contentView.addSubview(cancelButton)
        
        confirmButton.setTitle("确认", for: .normal)
        confirmButton.setTitleColor(UIColor.white, for: .normal)
        confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        confirmButton.addTarget(self, action: #selector(confirmButtonClick), for: .touchUpInside)
        confirmButton.backgroundColor = ConfirmButtonColor
        contentView.addSubview(confirmButton)
        
        cancelButton.snp.makeConstraints { (make) in
            make.left.bottom.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        confirmButton.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalToSuperview().multipliedBy(0.5)
        }
    }
    
    func addTipContentView(childContentView:RJTipContentView) -> () {
        self.childContentView = childContentView
        self.contentView.addSubview(childContentView)
        self.dataSource = self.childContentView
        self.animationDelegate = self.childContentView
        self.contentView.setupCornerRadius(2)
        self.childContentView.tipView = self
        
        self.childContentView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            if showButton {
                make.bottom.equalToSuperview().offset(-50)
            }else{
                make.bottom.equalToSuperview()
            }
        }
    }
}

//MARK: 消失展示方法
extension RJTipPopView {
    
    fileprivate func showTipView() -> () {
        guard let window = UIApplication.shared.delegate?.window else { return }
        window?.rootViewController?.view.addSubview(self)
        
        self.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.animationDelegate?.RJTipPopViewAnimationDelegateWithOpen(view: self.contentView)
        
    }
    
    @objc func dismiss() -> () {
        self.animationDelegate?.RJTipPopViewAnimationDelegateWithDismiss(view: self.contentView)
    }
    
}



class RJTipContentView: UIView,RJTipPopViewDataSource,RJTipPopViewAnimationDelegate {

    var tipView: RJTipPopView!
    
    func RJTipPopViewDataSourceWithCompletion() -> Any {
        return "代理值"
    }
    
    func RJTipPopViewAnimationDelegateWithOpen(view: UIView) {
        let animation = CABasicAnimation(keyPath: "transform.scale")
        animation.fromValue = NSNumber(value: 0.8)
        animation.toValue = NSNumber(value: 1)
        animation.duration = 0.15
        animation.autoreverses = false
        animation.repeatCount = 1
        animation.isRemovedOnCompletion = true
        animation.fillMode = .forwards

        view.layer.add(animation, forKey: "animation")
    }
    
    func RJTipPopViewAnimationDelegateWithDismiss(view: UIView) {
        UIView.animate(withDuration: 0.35) {
            let animation = CABasicAnimation(keyPath: "transform.scale")
            animation.fromValue = NSNumber(value: 1)
            animation.toValue = NSNumber(value: 0.1)
            animation.duration = 0.45
            animation.autoreverses = false
            animation.repeatCount = 1
            animation.isRemovedOnCompletion = false
            animation.fillMode = .forwards

            view.layer.add(animation, forKey: "animation")
            view.alpha = 0
        } completion: { (result) in
            if result {
                self.tipView.removeFromSuperview()
            }
        }
    }
    
}
